package tech.com.commoncore;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;

import tech.com.commoncore.basecomponent.ServiceFactory;
import tech.com.commoncore.user.CurrentUser;

// 拦截器会在跳转之间执行，多个拦截器会按优先级顺序依次执行
@Interceptor(priority = 1) // priority 优先级
public class LoginInterceptorImpl implements IInterceptor {
    public static final int LOGINCHECK = 0;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if (postcard.getExtra() == LOGINCHECK) { // 只处理加标记的activity，否则直接进入
            CurrentUser user = ServiceFactory.getInstance().getLoginService().getCurrentUser();
            boolean isLogin = (user != null && user.isLogin());
            if (isLogin) {
                callback.onContinue(postcard);
            } else {
                callback.onInterrupt(null);
                ARouter.getInstance().build("/login/login")  //重定向到登录页面
                        .with(postcard.getExtras())
                        .withString("path", postcard.getPath())
                        .greenChannel()
                        .navigation();
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
    }
}